//
//  TaskNewView.swift
//  SwiftUILean
//
//  Created by wyknmjj on 2024/5/15.
//

import SwiftUI

struct TaskNewView:View {
    @Environment(\.dismiss) private var dismiss
    @State private var taskTitle = ""
    @State private var taskDate = Date.init()
    @State private var taskColor = "TaskColor1"
    @Environment(\.modelContext) private var context
    var body: some View {
        VStack(alignment: .leading,spacing: 15){
            Button{
                dismiss()
            }label: {
                Image(systemName: "xmark.circle.fill")
                    .font(.title)
                    .tint(.red)
            }
            .hSpacing(.leading)
            
            VStack(alignment: .leading,spacing: 8){
                Text("Task Title")
                    .font(.caption)
                    .foregroundStyle(.gray)
                
                TextField("Go for a Walk!",text: $taskTitle)
                    .padding(.vertical,12)
                    .padding(.horizontal,15)
                    .background(.white.shadow(.drop(color:.black.opacity(0.25),radius: 2)),in: .rect(cornerRadius: 10))
            }
            .padding(.top,5)
            
            HStack(spacing: 12){
                VStack(alignment: .leading,spacing: 8){
                    Text("Task Date")
                        .font(.caption)
                        .foregroundStyle(.gray)
                    
                    DatePicker("",selection: $taskDate)
                        .datePickerStyle(.compact)
                        .scaleEffect(0.9,anchor: .leading)
                }.padding(.top,8)
                //给颜色选择更多的空间
                    .padding(.trailing,-15)
                VStack(alignment: .leading,spacing: 8){
                    Text("Task Color")
                        .font(.caption)
                        .foregroundStyle(.gray)
                    
                    let colors:[String]=(1...4).map { index in
                        return "TaskColor\(index)"
                    }
                    
                    HStack(spacing:0){
                        ForEach(colors,id:\.self){color in
                                Circle()
                                .fill(getTintColor(color))
                                .frame(width:20,height: 20)
                                .background{
                                    Circle()
                                        .stroke(lineWidth: 2)
                                        .opacity(taskColor == color ? 1 : 0)
                                }
                                .hSpacing(.center)
                                .onTapGesture {
                                    withAnimation(.snappy) {
                                        taskColor=color
                                    }
                                }
                        }
                    }
                }
            }.padding(.top,8)
            Spacer(minLength: 0)
            Button{
                let task = TaskItem(taskTitle: taskTitle,
                                    creationDate: taskDate,
                                    tInt: taskColor)
                do{
                    context.insert(task)
                    try context.save()
                    dismiss()
                }catch{
                    print(error.localizedDescription)
                }
            }label: {
                Text("Create Task")
                    .font(.title3)
                    .fontWeight(.semibold)
                    .textScale(.secondary)
                    .foregroundStyle(.black)
                    .hSpacing(.center)
                    .padding(.vertical,12)
                    .background(getTintColor(taskColor),in: .rect(cornerRadius: 10))
            }
            .disabled(taskTitle == "")
            .opacity(taskTitle=="" ? 0.5 : 1)
        }.padding(15)
    }
}


#Preview {
    TaskNewView()
        .vSpacing(.bottom)
}
